package com.antdesignbackend.backend.situationReport.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.antdesignbackend.backend.config.WebConfig;
import com.antdesignbackend.backend.form.animalApplication.entity.AnimalApplication;
import com.antdesignbackend.backend.production.reservation.entity.entityVo.ReservationVo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.situationReport.entity.SituationReport;
import com.antdesignbackend.backend.situationReport.entity.entityVo.SituationReportVo;
import com.antdesignbackend.backend.situationReport.service.SituationReportService;

/**
 * 动物防疫条件和防疫制度执行情况报备
 *
 * @author 林宇杰
 * @email situationReport
 * @date 2020-08-31 10:28:17
 */

@Controller
@RequestMapping("/situationReport")
@Slf4j
public class SituationReportController extends BaseController{

	private static final String FUN_NAME = "动物防疫条件和防疫制度执行情况报备";//功能名称

	@Autowired
	private SituationReportService situationReportService;

	@GetMapping("/index")
	@AuthToken
	public String index(Model model) {
		model.addAttribute("userType",getUserType());
		return "situationReport/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody SituationReportVo situationReportVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result=new ReturnVO();
		List<SituationReportVo> situationReportVos=new ArrayList<>();
		QueryWrapper<SituationReportVo> wrapper =new QueryWrapper<>();
		if(situationReportVo.getReportYear()!=null && !situationReportVo.getReportYear().equals("")){
			wrapper.eq("reportYear",situationReportVo.getReportYear());
		}
		if(getUserType().equals("3")){	//养殖企业
			wrapper.eq("a.eid",getLoginEnterpriseId());
		}
		wrapper.orderByDesc("reportYear");
		//先统计满足条件的数据总数，因为sql 不包括排序
		int total=situationReportService.count(wrapper);
		result.setTotal(total);
		//再查询
		if(total>=0)
		{
			situationReportVos=situationReportService.search(wrapper,new Page<>(situationReportVo.getPageIndex(),situationReportVo.getPageSize()));
			for(SituationReportVo situationReportVo1:situationReportVos){
				//去掉内容html标签，用于显示
				if (situationReportVo1.getContent() != null){
					String showContent = situationReportVo1.getContent().replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "");
					if (showContent.length() > 7){
						showContent = showContent.substring(0,7) + "...";
					}
					situationReportVo1.setShowContent(showContent);
				}
				//拼接文件路径
				if (situationReportVo1.getEnclosure()!=null && !situationReportVo1.getEnclosure().equals(""))
					situationReportVo1.setEnclosure(WebConfig.host + "enclosure/" + situationReportVo1.getEnclosure());
			}
		}
		result.setData(situationReportVos);
		return result;
	}

	@GetMapping("/getById/{id}")
	@ResponseBody
	public	SituationReport getById(@PathVariable(name = "id") String id) {
		SituationReport obj = situationReportService.getById(id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody SituationReport situationReport){
		ReturnVO result = new ReturnVO();
		situationReport.setEid(getLoginEnterpriseId());
		situationReport.setUid(getLoginUserId());
		situationReport.setCreateTime(new Date());
		if(situationReportService.save(situationReport)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody SituationReport situationReport){
		ReturnVO result = new ReturnVO();
		if(situationReportService.updateById(situationReport)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (situationReportService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

}
